package com.hmall.item.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.SearchDTO;
import com.hmall.common.pojo.Item;
import com.hmall.common.vo.SearchVo;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Override
    public PageDTO<Item> getPage(Integer page, Integer size) {

        //使用ItemService中的lambdaQuery方法分页查询
        Page<Item> itemPage = new Page<>(page,size);
        Page<Item> pageQuery = this.page(itemPage);
        return new PageDTO<>(pageQuery.getTotal(),pageQuery.getRecords());

    }

    /**
     * 修改商品
     * @param item
     */
    @Override
    public void updateGood(Item item) {
        lambdaUpdate().set(Item::getName,item.getName())
                .set(Item::getCategory,item.getCategory())
                .set(Item::getBrand,item.getBrand())
                .set(Item::getPrice,item.getPrice())
                .set(Item::getStock,item.getStock())
                .set(Item::getSpec,item.getSpec())
                .set(Item::getImage,item.getImage())
                .set(Item::getIsAD,item.getIsAD())
                .set(Item::getUpdateTime, LocalDateTime.now())
                .eq(Item::getId,item.getId())
                .update();
    }

    /**
     * 上架下架
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        lambdaUpdate().set(Item::getStatus,status)
                .set(Item::getUpdateTime,LocalDateTime.now())
                .eq(Item::getId,id)
                .update();
    }

    @Override
    public SearchVo searchFilters() {

        List<Item> list = lambdaQuery().select(Item::getCategory)
                .groupBy(Item::getCategory)
                .list();
        List<String> category = list.stream().map(Item::getCategory).collect(Collectors.toList());
        List<Item> list1 = lambdaQuery().select(Item::getBrand)
                .groupBy(Item::getBrand)
                .list();
        List<String> brand = list1.stream().map(Item::getBrand).collect(Collectors.toList());
        SearchVo searchVo = new SearchVo();
        searchVo.setCategory(category.toArray(new String[0]));
        searchVo.setBrand(brand.toArray(new String[0]));
        return searchVo;
    }

    @Override
    public PageDTO<Item> searchList(SearchDTO dto) {
        Page<Item> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Item> itemLambdaQueryWrapper = Wrappers.<Item>lambdaQuery().select()
                .like(Item::getName, dto.getKey())
                .eq(ObjectUtil.isNotEmpty(dto.getCategory()),Item::getCategory,dto.getCategory())
                .eq(ObjectUtil.isNotEmpty(dto.getBrand()),Item::getBrand,dto.getBrand())
                .orderByDesc(Item::getPrice);



        Page<Item> itemPage = page(page, itemLambdaQueryWrapper);
        if(StringUtils.isEmpty(dto.getKey())&&dto.getPage()==1&&dto.getCategory()==null&&dto.getBrand()==null){
            String top20 = redisTemplate.opsForValue().get("top20");
            return new PageDTO<>(itemPage.getTotal(), JSONUtil.toList(top20, Item.class));
        }

        return new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
    }

    /**
     * 销量前20
     */
    @Override
    public List<Item> getTop20() {

        List<Item> list = lambdaQuery().select()
                .orderByDesc(Item::getSold)
                .list();
        return list.subList(0,20);
    }

    /**
     * 扣库存
     * @param itemId
     * @param num
     */
    @Override
    public void updateStock(Long itemId, Integer num) {
        Item item = getById(itemId);
        lambdaUpdate().
                set(item.getStock()>num,Item::getStock,item.getStock()-num)
                .set(Item::getSold,item.getSold()+num)
                .set(Item::getUpdateTime,LocalDateTime.now())
                .eq(Item::getId,itemId).update();
    }
    /**
     * 恢复库存
     * @param itemId
     * @param num
     */
    @Override
    public void updateRestock(Long itemId, Integer num) {
        Item item = getById(itemId);

        lambdaUpdate().
                set(Item::getStock,item.getStock()+num)
                .set(Item::getUpdateTime,LocalDateTime.now())
                .eq(Item::getId,itemId)
                .update();
    }


}
